package com.jrzn.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.jrzn.adapter.InforAdapter;
import com.nsjr.jrznandroid.R;
import com.nsjr.jrznandroid.nsjr.zixun.ZixunActivity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by haokai on 2016/5/12.
 */
public class InfoFragment extends BaseFragment implements ViewPager.OnPageChangeListener {
    private View view;
    private RadioButton radioButton1,radioButton2;
    private RadioGroup radioGroup;
    private ViewPager viewPager;
    private LinearLayout linearpoints;
    private List<ImageView> imageViews;
    private ListView listview;
    private InforAdapter inforAdapter;
    private List<String>listdata=new ArrayList<>();
   private PullToRefreshScrollView pullToRefreshScrollView;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_info, null);
        init();
        return view;
    }

    private void init(){
        radioButton1=(RadioButton)view.findViewById(R.id.tab_info_radiobutton1);
        radioButton2=(RadioButton)view.findViewById(R.id.tab_info_radiobutton2);
        radioGroup=(RadioGroup)view.findViewById(R.id.tab_info_radiogroup);
        viewPager=(ViewPager)view.findViewById(R.id.adv_pager_info);
        linearpoints=(LinearLayout)view.findViewById(R.id.liear_points_info);
        listview=(ListView)view.findViewById(R.id.list_info_data);
        pullToRefreshScrollView=(PullToRefreshScrollView)view.findViewById(R.id.pull_scrollivew);
         pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                RefreshComplete();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                RefreshComplete();
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(radioButton1.getId()==checkedId){

                }else if(checkedId==radioButton2.getId()){

                }
            }
        });
        imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < 3; i++) {
            ImageView image = new ImageView(getActivity());
            image.setBackgroundResource(R.mipmap.banner);
            imageViews.add(image);
            ImageView imagepoint = new ImageView(getActivity());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    20, 20);
            params.setMargins(5,5,5,5);
            imagepoint.setLayoutParams(params);
            imagepoint.setBackgroundResource(i == 0 ? R.drawable.rounded
                    : R.drawable.round);
            linearpoints.addView(imagepoint);
        }
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.setOnPageChangeListener(this);
        for (int i=0;i<5;i++){
            listdata.add("");
        }
        inforAdapter=new InforAdapter(activity,listdata);
        listview.setAdapter(inforAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent();
                intent.setClass(activity, ZixunActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    @Override
    public void onPageSelected(int position) {
        changeDot1(position);
    }
    @Override
    public void onPageScrollStateChanged(int state) {

    }
    class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imageViews.size();
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViews.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }
    }

    private void changeDot1(int index) {
        for (int i = 0; i < imageViews.size(); i++) {
            linearpoints.getChildAt(i).setBackgroundResource(R.drawable.round);
        }
        linearpoints.getChildAt(index).setBackgroundResource(R.drawable.rounded);
    }
    private void RefreshComplete(){
        /**
         * 判断是否停止刷新,加载
         *必须要在适配器绑定之后，才管用
         */
        pullToRefreshScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshScrollView.onRefreshComplete();
            }
        }, 1000);
    }
}